package com.binvshe.binvshe.mine;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.ParamKey;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.frameworkbase.utils.EasyToast;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;

public class FeedbackActivity extends BaseActivity {

    @Bind(R.id.mEtFeedback)
    EditText mEtFeedback;

    public static void start(Context context) {
        Intent starter = new Intent(context, FeedbackActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        enableBackButton();
        setTitle("意见反馈");
        mEtFeedback.requestFocus();
        getCustomToolbar().addRightButton("提交", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String feedBackText = mEtFeedback.getText().toString().trim();
                if (TextUtils.isEmpty(feedBackText)) {
                    EasyToast.showToast(FeedbackActivity.this, "反馈意见不能为空");
                    return;
                }
                if (feedBackText.length() > 300) {
                    EasyToast.showToast(FeedbackActivity.this, "限定字数300个");
                    return;
                }
                MyHttpRequest request = new MyHttpRequest(FeedbackActivity.this);
                JSONObject jobj = new JSONObject();
                try {
                    jobj.put(ParamKey.DESCRIPTION, feedBackText);

                    //0 建议 1意见 2 投诉 3其他
                    jobj.put(ParamKey.TYPE, 1 + "");
                    jobj.put(ParamKey.VERSION, getVersion());
                    request.post(UrlCenter.FEEDBACK, jobj, new DataRequestListener<Object>(Object.class) {
                        @Override
                        public void onStart(Context context, String url) {
                            enableProgress(false);
                            super.onStart(context, url);
                        }

                        @Override
                        public void onSuccess(Object o) {
                            super.onSuccess(o);
                            EasyToast.showToast(FeedbackActivity.this, "反馈成功~");
                            finish();
                        }

                        @Override
                        public void onFailure(Throwable t, int errorNo, String strMsg) {
                            super.onFailure(t, errorNo, strMsg);
                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public String getVersion() {
        PackageManager packageManager = getPackageManager();
        PackageInfo packInfo = null;
        String version = null;
        try {
            // getPackageName()是你当前类的包名，0代表是获取版本信息
            packInfo = packageManager.getPackageInfo(getPackageName(), 0);
            version = packInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return version;
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.act_feedback, null);
    }
}
